package com.wd.live.view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.Toast;


import com.wd.live.R;
import com.wd.live.base.BaseActivity;
import com.wd.live.base.BaseViewModel;
import com.wd.live.bean.RenZhengBean;
import com.wd.live.databinding.ActivityRenZhengBinding;
import com.wd.live.model.utils.ToastFormat;
import com.wd.live.view.activity.MainActivity;
import com.wd.live.viewmodel.RenZhengViewModel;

public class RenZhengActivity extends BaseActivity<RenZhengViewModel, ActivityRenZhengBinding> {

    int sex1=1;
    @Override
    protected void initData() {
        Intent intent = getIntent();
        String actualName = intent.getStringExtra("actualName");
        String sex = intent.getStringExtra("sex");
        if(sex.equals("男")){
            sex1=1;
        }else{
            sex1=2;
        }

        String idNumber = intent.getStringExtra("idNumber");
        String idcard = hideId(idNumber);
        databind.rzName.setText(actualName);
        databind.rzsex.setText(sex);
        databind.rzId.setText(idcard);
        databind.renzheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                viewmodel.getList(actualName,sex1,"2002-05-10","黑龙江省大庆市萨尔图区纬九路6-28号楼2单元",
                        idNumber,"大庆市公安局萨尔图分局","2029-07-22","application/json;charset=UTF-8"
                ).observe(RenZhengActivity.this, new Observer<RenZhengBean>() {
                    @Override
                    public void onChanged(RenZhengBean renZhengBean) {
                        ToastFormat.setText(renZhengBean.getMessage());
                        ToastFormat.show();
                        if(renZhengBean.getStatus().equals("0000")){
                            View view1 = LayoutInflater.from(RenZhengActivity.this).inflate(R.layout.popup_rzsussce, null, false);
                            PopupWindow popupWindow = new PopupWindow(view1, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                            popupWindow.showAtLocation(view1, Gravity.CENTER,0,0);
                            view1.findViewById(R.id.liveGo).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    popupWindow.dismiss();
                                    finish();
                                }
                            });
                            view1.findViewById(R.id.backGo).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    startActivity(new Intent(RenZhengActivity.this, MainActivity.class));
                                    finish();
                                    popupWindow.dismiss();
                                }
                            });
                        }
                    }
                });
            }
        });
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_ren_zheng;
    }

    @Override
    public void onChanged(Object o) {

    }
    public static String hideId(String id) {
        String idNum = id.replaceAll("(\\d{4})\\d{10}(\\d{4})", "$1****$2");
        return idNum;
    }
}